package com.joker.aigc.blog.controller;

import cn.hutool.core.lang.Assert;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.joker.aigc.blog.domain.dto.ArticleCommentDTO;
import com.joker.aigc.blog.domain.dto.ArticleCommentQueryDTO;
import com.joker.aigc.blog.domain.vo.ArticleCommentVO;
import com.joker.aigc.blog.service.ArticleCommentService;
import com.joker.aigc.domain.entity.R;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
@RequestMapping("/api/1.0/article-comments")
public class ArticleCommentController {

    @Resource
    private ArticleCommentService articleCommentService;

    /**
     * 根据文章id分页获取评论信息
     *
     * @param articleCommentQueryDTO 查询条件
     * @return 分页评论信息
     */
    @PostMapping("/by-article")
    public R<Page<ArticleCommentVO>> getCommentsByArticleId(@RequestBody ArticleCommentQueryDTO articleCommentQueryDTO) {
        Assert.notNull(articleCommentQueryDTO.getArticleId(), "文章id不能为空");
        return R.ok(articleCommentService.getCommentsByArticleId(articleCommentQueryDTO));
    }

    /**
     * 根据评论id获取子评论信息
     *
     * @param articleCommentQueryDTO 查询条件
     * @return 子评论信息
     */
    @PostMapping("/sub-comments")
    public R<Page<ArticleCommentVO>> getSubCommentsByCommentId(@RequestBody ArticleCommentQueryDTO articleCommentQueryDTO) {
        Assert.notNull(articleCommentQueryDTO.getId(), "评论id不能为空");
        return R.ok(articleCommentService.getSubCommentsByCommentId(articleCommentQueryDTO));
    }

    /**
     * 添加评论
     *
     * @param articleCommentDTO 评论信息
     */
    @PostMapping("addComment")
    public R<String> addComment(@RequestBody ArticleCommentDTO articleCommentDTO) {
        Assert.notNull(articleCommentDTO.getArticleId(), "文章id不能为空");
        Assert.notNull(articleCommentDTO.getContent(), "评论内容不能为空");
        Assert.notNull(articleCommentDTO.getRootId(), "主评论不能为空");
        articleCommentService.addComment(articleCommentDTO);
        return R.ok("评论成功");
    }
}
